﻿namespace Borg
{
    public abstract class RepositoryMetaElement : RepositoryElement
    {
        internal QualificationState qualificationState;

        public QualificationState QualificationState
        {
            get { return qualificationState; }
        }

        public abstract bool IsQualified { get; }

        public virtual bool IsQualificationMissing
        {
            get
            {
                return (qualificationState &
                    QualificationState.QualificationMissing) ==
                    QualificationState.QualificationMissing;
            }
        }

        protected RepositoryMetaElement()
        {
            qualificationState =
                QualificationState.QualificationMissing |
                QualificationState.MethodQualificationMissing;
        }

        public bool Qualify()
        {
            return IsQualified || ForceQualify();
        }

        public bool ForceQualify()
        {
            var qualificationMissing = IsQualificationMissing;
            var isQualified = IsQualified;

            InternalQualify();

            if (qualificationMissing != IsQualificationMissing)
                OnPropertyChanged("IsQualificationMissing");

            if (isQualified != IsQualified)
                OnPropertyChanged("IsQualified");

            return IsQualified;
        }

        internal abstract QualificationState InternalQualify();
    }
}

